package com.wc.alorithm_luogu.P2036;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;

/**
 * @Author congge
 * @Date 2023/9/26 21:28
 * @description [COCI2008-2009#2] PERKET
 * https://www.luogu.com.cn/problem/P2036
 */
public class Main {
    public static void main(String[] args) {
        Input scan = new Input();
        int n = scan.nextInt();
        int[] s = new int[n];
        int[] b = new int[n];
        for (int i = 0; i < n; i++) {
            s[i] = scan.nextInt();
            b[i] = scan.nextInt();
        }
        Main main = new Main();

        System.out.println(main.minSubtract(s, b));
    }


    public long minSubtract(int[] s, int[] b) {
        int sour = 1;
        int bitter = 0;
        /**
         * 检查是否全没加
         */
        return minSubtract(s, b, 0, sour, bitter, false);
    }

    /**
     * 计算最小差值
     *
     * @param s      酸度数组
     * @param b      哭度数组
     * @param index  当前位置
     * @param sour   当前酸值
     * @param bitter 当前哭值
     * @param flg    检查是否全没加
     * @return
     */
    public long minSubtract(int[] s, int[] b, int index, int sour, int bitter, boolean flg) {
        if (index == s.length) {
            /**
             * 如果全没加，就返回最小的差值
             */
            if (!flg) {
                int min = Integer.MAX_VALUE;
                for (int i = 0; i < s.length; i++) {
                    min = Math.min(min, Math.abs(s[i] - b[i]));
                }
                return min;
            }
            if (sour > bitter) {
                return sour - bitter;
            } else {
                return bitter - sour;
            }
        }
        long addMinSubtract = minSubtract(s, b, index + 1, sour * s[index], bitter + b[index], true);
        long notAddMinSubtract = minSubtract(s, b, index + 1, sour, bitter, flg);
        return Math.min(addMinSubtract, notAddMinSubtract);
    }

    static class Input {
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public int nextInt() {
            try {
                in.nextToken();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return (int) in.nval;
        }

    }
}
